import urllib.request
import urllib.parse
import json

content= input('请输入需要翻译的内容: ')

#translate_o   吧_o去掉可以爬虫 url='http://fanyi.youdao.com/translate_o?smartresult=dict&smartresult=rule' 
url='http://fanyi.youdao.com/translate?smartresult=dict&smartresult=rule' 


# 下面这些都先按照我们之前抓包获取到的数据
data={}
data['i']= content
data['from']= "AUTO"
data['to']= "AUTO"
data['smartresult']= "dict"
data['client']= "fanyideskweb"
data['salt']= "15647463093426"
data['sign']= "47521708ac619a162f57370325807e31"
data['ts']= "1564746309342"
data['bv']= "ce1fe729a12a6b5169046dd3aac11e4c"
data['doctype']= "json"
data['version']= "2.1"
data['keyfrom']= "fanyi.web"
data['action']= "FY_BY_CLICKBUTTION"

# 对数据进行编码处理
data=urllib.parse.urlencode(data).encode('utf-8')

response=urllib.request.urlopen(url,data)
html=response.read().decode('utf-8')

target=json.loads(html)
print("翻译结果:%s" %(target['translateResult'][0][0]['tgt']))